define(function (require,exports,module) {
    var angular = require('angular');

    angular.module('common',[])
        .filter('fileSize',[
            function () {
                return function (size,unit) {
                    var sizeText;
                    unit = (unit || "K").toUpperCase();
                    switch (unit) {
                        case "B":
                            sizeText = size;
                            break;
                        case "K":
                            sizeText = size / 1024;
                            break;
                        case "M":
                            sizeText = size / (1024 * 1024);
                            break;
                        case "G":
                            sizeText = size / (1024 * 1024 * 1024);
                            break;
                        default:
                            sizeText = size;
                            unit = "B";
                    }

                    return sizeText.toFixed(2) + unit;
                };
            }
        ])
        .factory('url',[
            function () {
                return {
                    addParam: function (url,search) {
                        for (var key in search) {
                            if (search.hasOwnProperty(key) && search[key]) {
                                var connecter = "&";
                                if (url.indexOf('?') === -1) {
                                    connecter = "?";
                                }
                                url += connecter + key + '=' + search[key];
                            }
                        }
                        return url;
                    }
                };
            }
        ])
        .factory('http',[
            'apife',
            'apibe',
            'url',
            '$http',
            function (apife,apibe,url,$http) {
                var requrl = apife + apibe;
                return {
                    get: function (api,search) {
                        api = url.addParam(requrl + api,search);
                        return $http.get(api);
                    },
                    post: function (api,data,search) {
                        var dataMap = [];
                        for (var key in data) {
                            if (data.hasOwnProperty(key)) {
                                var val = data[key];
                                if (angular.isObject(val)) {
                                    val = JSON.stringify(val);
                                }
                                dataMap.push(key + '=' + encodeURIComponent(val));
                            }
                        }
                        data = dataMap.join('&');
                        api = url.addParam(requrl + api,search);
                        return $http.post(api,data,{
                            headers: {
                                'Content-Type':'application/x-www-form-urlencoded'
                            }
                        });
                    }
                };
            }
        ])
});
